import {ref, computed} from "vue";
import  {subject} from '@/api'
const isShowError = ref(false) // 错题是否自动移除
const ContentRow = ref({})// //储存当前行内容
export default function useExam() { // isList 是否是瀑布流处理
    const isTab = ref(true)// 是否是tab切换
    const tabIndex = ref(0) // tab索引
    const isotype = ref(1)//1:顺序练习,2:随机练习
    const categoryId = ref('')//分类id
    const count = ref(0) //当前题目数量
    const currentIndex = ref(0)// 当前显示的题目索引
    const list = ref([])// 列表数据
    const showDialog = ref(false) // 是否显示错题详情弹窗
    const storageArr = ref([]) // 接口缓存数据

    const questionType = {
        '单选':'/static/images/danxuan.png',
        '多选':'/static/images/duoxuan.png',
        '判断':'/static/images/panduan.png',
        '1':'/static/images/danxuan.png',
        '2':'/static/images/duoxuan.png',
        '3':'/static/images/panduan.png',
    }
    const levelType = {
        '1': '简单',
        '2': '一般',
        '3': '难'
    }
    const titleType = {
        '1': '顺序练习',
        '2': '随机练习',
        '3': '我的收藏',
        '4': '我的错题',
        '5': '答题记录',
    }
    //错题数仅用于显示
    const errorCount = computed(() => {
         const errorList =  list.value.filter(item => item.down&&item.down != item.answer)
        return errorList
    })
    //对题数仅用于显示
    const successCount = computed(() => {
        const rightList =  list.value.filter(item => item.down&&item.down == item.answer)
        return rightList
    })
    //错题缓存数组，仅用于接口参数
    const errorStorageArr = computed(() => {
        //兼容旧版本，过滤出错题
        let arr3 = []
        const errorList = list.value.filter(item => item.down && item.down != item.answer)
        //查询缓存中的错题
        const obj = storageArr.value.find(item => item.storageKey === "errorids2")
        if(obj){
            let arr = JSON.parse(obj.storageValue)
            if(arr.length>0){
                arr3 = arr
                errorList.forEach(item1 => {
                    if(arr3.indexOf((item1.id).toString()) === -1){
                        arr3.push((item1.id).toString())
                    }
                })
                return arr3
            }else {
                return errorList.map(item=>(item.id).toString())
            }
        }else {
           return errorList.map(item=>(item.id).toString())
        }
    })
    //根据order2试题回显
    const order2List = computed(() => {
        const obj = storageArr.value.find(item => item.storageKey === `order${Number(isotype.value+1)}`)
        return obj || {}
    })
    //接口新增缓存
    const addStorageApi =(params)=>{
        subject.getSubjectPractice(params).then(data=>{
        })
    }
    return {
        isTab,
        tabIndex,
        isotype,
        categoryId,
        count,
        currentIndex,
        list,
        questionType,
        levelType,
        errorCount,
        successCount,
        showDialog,
        storageArr,
        errorStorageArr,
        order2List,
        titleType,
        isShowError,
        ContentRow,
        addStorageApi
    }
}